﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ContactSMS.Client.Models;
using ContactSMS.Client.Service;
using ContactSMS.Client.Views;

namespace ContactSMS.Client.ViewModels
{
    public partial class LoginViewModel : ObservableObject
    {
        private readonly LoginService loginService;

        [ObservableProperty]
        private string userName;

        [ObservableProperty]
        private string passWord;

        [ObservableProperty]
        private RegisterModel registerModel;

        public LoginViewModel(LoginService loginService)
        {
            this.loginService = loginService;
            registerModel = new RegisterModel();
        }

        [RelayCommand]
        public async Task Login()
        {
            if (string.IsNullOrEmpty(UserName))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "用户名不能为空", "确定");
                return;
            }

            if (string.IsNullOrEmpty(PassWord))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "密码不能为空", "确定");
                return;
            }

            var result = await loginService.LoginAsync(UserName, PassWord);
            if (result != null && result.Success)
            {
                var accessToken = result.Data.AccessToken;
                var refreshToken = result.Data.RefreshToken;
                await AuthService.LogInAsync(accessToken, refreshToken);
                //if (Shell.Current == null)
                //{
                //    await Application.Current.MainPage.DisplayAlert("错误", "Shell.Current 未初始化", "确定");
                //    return;
                //}
                try
                {
                    Application.Current.MainPage = new AppShell();
                }
                catch (Exception ex)
                {
                    await Application.Current.MainPage.DisplayAlert("导航失败", ex.Message, "确定");
                }
            }
            else
            {
                await Application.Current.MainPage.DisplayAlert("登录失败", result?.Message ?? "登录出错，请重试", "确定");
            }
        }

        [RelayCommand]
        public async Task Register()
        {
            if (string.IsNullOrEmpty(RegisterModel.UserName))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "用户名不能为空", "确定");
                return;
            }

            if (string.IsNullOrEmpty(RegisterModel.PassWord))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "密码不能为空", "确定");
                return;
            }

            if (string.IsNullOrEmpty(RegisterModel.PassWordTwo))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "密码不能为空", "确定");
                return;
            }

            if (!string.Equals(RegisterModel.PassWord, RegisterModel.PassWordTwo))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "两次密码不一样", "确定");
                return;
            }

            if (string.IsNullOrEmpty(RegisterModel.Email))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "邮箱不能为空", "确定");
                return;
            }

            if (string.IsNullOrEmpty(RegisterModel.Phone))
            {
                await Application.Current.MainPage.DisplayAlert("错误", "邮箱不能为空", "确定");
                return;
            }

            var result = await loginService.RegisterAsync(RegisterModel);
            if (result != null && result.Success)
            {
                try
                {
                    Application.Current.MainPage = new NavigationPage(new LoginPage());
                }
                catch (Exception ex)
                {
                    await Application.Current.MainPage.DisplayAlert("导航失败", ex.Message, "确定");
                }
            }
            else
            {
                await Application.Current.MainPage.DisplayAlert("注册失败", result?.Message ?? "注册出错，请重试", "确定");
            }
        }
    }
}
